//导入

var homehref = '';
$(function(){
    Page.initCss();
    Page.key = urls.GetQueryString('key');
    Page.id = urls.GetQueryString('mkey');

    var url_param = '?key=' + Page.key + '&mkey=' + Page.id;
    $('.tab-zongshu').attr('href', '../zongshu/' + url_param);
    $('.tab-canshu').attr('href', '../canshu/'  + url_param);

    if(Page.id) {
        Page.loadData();
    }
});

var Page = {
    id: false,
    name: false,
    parent_id: false,
    parent_name: false
};

Page.initCss = function() {
    var bodyWidth = Page.getWindowWidth();
    var bodyHeight = Page.getWindowHeight();
    $('.w-rest-160').width(bodyWidth - 180);
    // $('.param').css({'min-height': (bodyHeight - 360)+'px'})
}

Page.loadData = function() {
    var url = urls.server + '/models/' + Page.id + '/rates';
    $.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        success: function(e){
            if(!e || e.length < 1) {
                return;
            }
            var h = '', len = e.length;

            var count1 = 0, count2 = 0, count3 = 0;

            for(var i=0; i<len; i++) {
                var item = e[i];
                h += '<div class="branch">';
                h += '  <div class="lbl"><span>'+ item.name_cn +'</span></div>';
                h += '  <div class="content">';
                h += '      <div class="row">';
                h += '          <div class="c-blue s" style="width:'+ item.rate1 +'px"></div>';
                h += '          <div class="des">'+ item.rate1 +'%</div>';
                h += '      </div>';
                h += '      <div class="row">';
                h += '          <div class="c-yellow s" style="width:'+ item.rate2 +'px"></div>';
                h += '          <div class="des">'+ item.rate1 +'%</div>';
                h += '      </div>';
                h += '      <div class="row">';
                h += '          <div class="c-brown s" style="width:'+ item.rate2 +'px"></div>';
                h += '          <div class="des">'+ item.rate2 +'%</div>';
                h += '      </div>';
                h += '   </div>';
                h += '</div>';
                count1 += Number(item.rate1);
                count2 += Number(item.rate2);
                count3 += Number(item.rate3);
            }
            $('.seperate').after(h);

            if(len > 0) {
                var svg1 = Number(count1/len); 
                var svg2 = Number(count2/len); 
                var svg3 = Number(count3/len); 
                svg1 = svg1.toFixed(2);
                svg2 = svg2.toFixed(2);
                svg3 = svg3.toFixed(2);
                var html = '';
                html += '      <div class="row">';
                html += '          <div class="c-blue s" style="width:'+ svg1 +'px"></div>';
                html += '          <div class="des">'+ svg1 +'%</div>';
                html += '      </div>';
                html += '      <div class="row">';
                html += '          <div class="c-yellow s" style="width:'+ svg2 +'px"></div>';
                html += '          <div class="des">'+ svg2 +'%</div>';
                html += '      </div>';
                html += '      <div class="row">';
                html += '          <div class="c-brown s" style="width:'+ svg3 +'px"></div>';
                html += '          <div class="des">'+ svg3 +'%</div>';
                html += '      </div>';
                $('.svg-rate').html(html);
            }
        }
    });
}

Page.loadImg = function() {
    // var url = urls.server;
    // url += '/MotoModelSeries?';
    // url += '&filter[fields][listImage]=true';
    // url += '&filter[where][id]=' + Page.id;

    
    var url = urls.server;
    // url += '/MotoModelSeries?filter[fields][id]=true&filter[fields][name]=true';
    // url += '&filter[fields][listImage]=true&filter[fields][brand]=true&filter[fields][cat]=true';
    // url += '&filter[fields][rate]=true';
    // url += '&filter[limit]=50&filter[where][id]=' + Page.id;

    url += '/MotoModels/' + Page.id;

    $.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        success: function(e){
            if(e && e.listImage) {
                $('.preview-list-img').attr({'src': e.listImage});
            }
        }
    });
}









//获取页面显示区域高度
Page.getWindowHeight = function() {
    if (navigator.appName == "Microsoft Internet Explorer" ) {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    }
    else {
        return self.innerHeight;
    } 
}

//获取页面显示区域宽度
Page.getWindowWidth = function() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    }
    else {
        return self.innerWidth;
    } 
}

